package com.itheima.reggie.controller;

import com.itheima.reggie.common.ResultInfo;
import com.itheima.reggie.domain.Category;
import com.itheima.reggie.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author zsk
 * @create 2022-03-01-11:25
 */
@RestController
public class CategoryController {

    //依赖注入
    @Autowired
    private CategoryService categoryService;

    /*
    查询分类列表
    Request URL: http://localhost:8080/category/findAll
    Request Method: GET
     */
    @GetMapping("/category/findAll")
    public ResultInfo findAll(){

        //调用service层查询
        List<Category> categoryList =  categoryService.findAll();
        //返回结果
        return ResultInfo.success(categoryList);

    }

    /*
    新增菜品分类
    Request URL: http://localhost:8080/category
    Request Method: POST
     */
    @PostMapping("/category")
    public ResultInfo save(@RequestBody Category category){

        //调用service层添加
        categoryService.save(category);
        //返回结果
        return ResultInfo.success(null);

    }

    /*
    修改菜品分类信息
    Request URL: http://localhost:8080/category
    Request Method: PUT
     */
    @PutMapping("/category")
    public ResultInfo update(@RequestBody Category category){

        //调用service层修改
        categoryService.update(category);
        //返回结果
        return ResultInfo.success(null);
    }

    /*
    删除菜品分类信息
    Request URL: http://localhost:8080/category?id=1498534138544787456
    Request Method: DELETE
     */
    @DeleteMapping("/category")
    public ResultInfo deleteById(Long id){
        //调用service层删除
        return categoryService.deleteById(id);
    }


    /*
    回显菜品分类信息
    Request URL: http://localhost:8080/category/list?type=1
    Request Method: GET
     */
    @GetMapping("/category/list")
    public ResultInfo findByType(Integer type){
        //调用service层查询
        List<Category> categoryList = categoryService.findByType(type);
        //返回结果
        return ResultInfo.success(categoryList);

    }

}
